/* $Id: JCreateMapDialog.java 63 2010-02-14 13:52:34Z aaron.day $ */
package org.crazydays.gameplan.map.swing;


import java.awt.Frame;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JDialog;

import org.crazydays.gameplan.controller.Controller;
import org.crazydays.gameplan.map.GameMap;
import org.crazydays.gameplan.view.View;
import org.crazydays.i18n.Messages;

/**
 * JCreateMapDialog
 */
public class JCreateMapDialog
    extends JDialog
    implements ActionListener
{
    /** serial version uid */
    public final static long serialVersionUID =
        "$Id: JCreateMapDialog.java 63 2010-02-14 13:52:34Z aaron.day $"
            .hashCode();

    /** messages */
    public static Messages MESSAGES = Messages.getInstance();

    /** resource key: title */
    public static String RESKEY_TITLE = "JCreateMapDialog.title";

    /** resource key: create */
    public static String RESKEY_LABEL_CREATE = "JCreateMapDialog.label.create";

    /** game map properties panel */
    protected JGameMapPropertiesPanel gameMapPropertiesPanel;

    /** controller */
    protected Controller controller;

    /** create */
    protected JButton create;

    /**
     * JCreateMap constructor.
     * 
     * @param view View
     */
    public JCreateMapDialog(View view)
    {
        super((Frame) view.getParent(), MESSAGES.format(RESKEY_TITLE), true);
    }

    /**
     * Set controller.
     * 
     * @param controller Controller
     */
    public void setController(Controller controller)
    {
        this.controller = controller;
    }

    /**
     * Initialize the form components.
     */
    public void initialize()
    {
        initializeGameMapPropertiesPanel();
        initializeCreate();
        initializeLayout();
        pack();
        initializeLocation();
    }

    /**
     * Initialize game map properties panel.
     */
    protected void initializeGameMapPropertiesPanel()
    {
        gameMapPropertiesPanel = new JGameMapPropertiesPanel();
        gameMapPropertiesPanel.initialize();
    }

    /**
     * Initialize create component.
     */
    protected void initializeCreate()
    {
        create = new JButton(MESSAGES.getMessage(RESKEY_LABEL_CREATE));
        create.addActionListener(this);
        getRootPane().setDefaultButton(create);
    }

    /**
     * Initialize layout.
     */
    public void initializeLayout()
    {
        GroupLayout layout = new GroupLayout(getContentPane());
        setLayout(layout);

        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);

        // horizontal
        layout.setHorizontalGroup(layout.createParallelGroup(
            GroupLayout.Alignment.TRAILING)
            .addComponent(gameMapPropertiesPanel).addComponent(create));

        // vertical
        layout.setVerticalGroup(layout.createSequentialGroup().addComponent(
            gameMapPropertiesPanel).addGroup(
            layout.createSequentialGroup().addComponent(create)));
    }

    /**
     * Initialize location.
     */
    protected void initializeLocation()
    {
        int x =
            getParent().getX() + (getParent().getWidth() / 2)
                - (getWidth() / 2);
        int y =
            getParent().getY() + (getParent().getHeight() / 2)
                - (getHeight() / 2);

        setLocation(new Point(x, y));
    }

    /**
     * Action performed.
     * 
     * @param event Event
     */
    @Override
    public void actionPerformed(ActionEvent event)
    {
        GameMap gameMap = gameMapPropertiesPanel.getGameMap();
        gameMap.initializeImage();
        controller.addGameMap(gameMap);

        setVisible(false);
        dispose();
    }
}
